﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Text;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Collections.Specialized;
using System.Windows.Markup;
using Sinacor.Infra.UI.Common;
using System.Reflection;
using System.Windows.Input;
using Sinacor.Infra.UI.Controls.SmartClient;
using System.Windows.Media;


namespace Sinacor.Infra.UI.Controls.SmartClient
{
    class ChangesMonitorManager : DependencyObject
    {
        #region Propriedades        

        public static readonly DependencyProperty IsChangeMonitoredProperty =
            DependencyProperty.Register("IsChangeMonitored", typeof(bool), typeof(ChangesMonitorManager), new FrameworkPropertyMetadata(true));

        #endregion

        #region ControlHasChanged

        public static RoutedEvent ControlHasChangedEvent = EventManager.RegisterRoutedEvent("ControlHasChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SinacorChangesMonitorContainer));

        public static RoutedEvent GridHasChangedEvent = EventManager.RegisterRoutedEvent("GridHasChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SinacorChangesMonitorContainer));

        #endregion

        /// <summary>
        /// Adds the changes handler.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="handler">The handler.</param>
        public static void AddChangesHandler(UIElement element, RoutedEventHandler handler)
        {
            element.AddHandler(ControlHasChangedEvent, handler);
        }

        public static void RemoveChangesHandler(UIElement element, RoutedEventHandler handler)
        {
            element.RemoveHandler(ControlHasChangedEvent, handler);
        }

        public static void AddGridChangesHandler(UIElement element, RoutedEventHandler handler)
        {
            element.AddHandler(GridHasChangedEvent, handler);
        }

        public static void RemoveGridChangesHandler(UIElement element, RoutedEventHandler handler)
        {
            element.RemoveHandler(GridHasChangedEvent, handler);
        }

        public static string GetDefaultPropertyAttribute(UIElement element) 
        {
            object[] attributes = null;
            attributes = element.GetType().GetCustomAttributes(typeof(DefaultPropertyAttribute), true);
            foreach (object attribute in attributes)
            {
                if (attribute is DefaultPropertyAttribute &&
                        !string.IsNullOrEmpty(((DefaultPropertyAttribute)attribute).Name))
                {
                    return ((DefaultPropertyAttribute)attribute).Name;
                    break;
                }
            }
            return "";
        }

        /// <summary>
        /// Recuperar o ChangesMonitorContainer no qual esta o elemento
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        internal static SinacorChangesMonitorContainer GetParentChangesMonitorContainer(FrameworkElement element)
        {
            DependencyObject container = element;
            do
            {
                container = VisualTreeHelper.GetParent(container); 
            } while (container != null && !(container is SinacorChangesMonitorContainer));

            return container as SinacorChangesMonitorContainer;
        }
    }

}
